package com.qf.hospital.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.DTO.AddRoleDTO;
import com.qf.hospital.DTO.SysRoleDTO;
import com.qf.hospital.VO.MenuDTreeVO;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.entity.SysRole;
import com.qf.hospital.entity.SysRoleExample;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.exception.HosptialException;
import com.qf.hospital.mapper.SysRoleMapper;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.service.SysRoleService;
import com.qf.hospital.util.ExampleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/18 17:52
 */

@Service
public class SysRoleServiceImpl implements SysRoleService {
    private Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Autowired
    private SysRoleMapper mapper;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查找所有角色
     * @return
     */
    @Override
    public List<SysRole> findAll() {
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andAvailableEqualTo(1);
        List<SysRole> sysRoleList = mapper.selectByExample(example);
        return sysRoleList;
    }

    /**
     * 根据条件查询所有角色
     * @param roleName 角色名称
     * @param roleDesc 角色备注
     * @param available 是否可用
     * @param page 当前页
     * @param limit 每页数据个数
     * @return
     */
    @Override
    public PageInfo<SysRole> findRoleByCondition(String roleName, String roleDesc, Integer available, Integer page, Integer limit) {
        //设置分页
        PageHelper.startPage(page, limit);
        //查询数据
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(roleName)){
            criteria.andRoleNameLike(ExampleUtil.any(roleName));
        }
        if (!StringUtils.isEmpty(roleDesc)){
            criteria.andRoleDescLike(ExampleUtil.any(roleDesc));
        }
        if (available!=null){
            criteria.andAvailableEqualTo(available);
        }
        List<SysRole> sysRoleList = mapper.selectByExample(example);
        //封装pageInfo
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoleList);
        return pageInfo;
    }

    /**
     * 修改角色信息
     * @param sysRoleDTO
     */
    @Override
    @Transactional
    public void update(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        int count = mapper.updateByPrimaryKeySelective(sysRole);
        if (count!=1){
            //记录日志
            log.error("【修改角色信息】 修改失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 添加角色信息
     * @param addRoleDTO
     */
    @Override
    @Transactional
    public void insertRole(AddRoleDTO addRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(addRoleDTO, sysRole);
        int count = mapper.insertSelective(sysRole);
        if (count!=1){
            //记录日志
            log.error("【添加角色信息】 修改失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 删除角色信息
     * @param roleid
     */
    @Override
    @Transactional
    public void deleteRole(Integer roleid) {
        int count = mapper.deleteByPrimaryKey(roleid);
        if (count!=1){
            //记录日志
            log.error("【删除角色信息】 删除失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 分配角色菜单
     * @param roleid
     * @return
     */
    @Override
    public List<MenuDTreeVO> initRoleMenuByRoleId(Integer roleid) {
        //查询所有菜单
        List<SysMenu> sysMenuList = sysMenuService.findAll();

        //查询该角色的所有菜单id
        List<Integer> mids = mapper.findMenuByRoleId(roleid);

        //封装
        List<MenuDTreeVO> data = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            MenuDTreeVO menuDTreeVO = new MenuDTreeVO();
            BeanUtils.copyProperties(sysMenu, menuDTreeVO);
            if (mids.contains(sysMenu.getId())){
                menuDTreeVO.setCheckArr("1");
            }
            data.add(menuDTreeVO);
        }
        return data;
    }

    /**
     * 添加角色菜单
     * @param roleid
     * @param ids
     */
    @Override
    @Transactional
    public void insertRoleMenu(Integer roleid, Integer[] ids) {
        //删除数据
        mapper.deleteRoleMenuByRoleId(roleid);

        //添加数据
        int count = mapper.insertRoleMenu(roleid,ids);
        if (count!=ids.length){
            //记录日志
            log.error("【添加角色菜单】 添加失败，roleid = {} , ids = {}",roleid,ids);
            //抛出异常
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION);
        }
    }


}
